<template>
  <el-dialog
    :title="!dataForm.mid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="收费时间" prop="mdate">
      <el-input v-model="dataForm.mdate" placeholder="收费时间"></el-input>
    </el-form-item>
    <el-form-item label="收费金额" prop="mcount">
      <el-input v-model="dataForm.mcount" placeholder="收费金额"></el-input>
    </el-form-item>
    <el-form-item label="收费对象id" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="收费对象id"></el-input>
    </el-form-item>
    <el-form-item label="收费人ID" prop="did">
      <el-input v-model="dataForm.did" placeholder="收费人ID"></el-input>
    </el-form-item>
    <el-form-item label="收费对象姓名" prop="pname">
      <el-input v-model="dataForm.pname" placeholder="收费对象姓名"></el-input>
    </el-form-item>
    <el-form-item label="收费人姓名" prop="dname">
      <el-input v-model="dataForm.dname" placeholder="收费人姓名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          mid: 0,
          mdate: '',
          mcount: '',
          pid: '',
          did: '',
          pname: '',
          dname: ''
        },
        dataRule: {
          mdate: [
            { required: true, message: '收费时间不能为空', trigger: 'blur' }
          ],
          mcount: [
            { required: true, message: '收费金额不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '收费对象id不能为空', trigger: 'blur' }
          ],
          did: [
            { required: true, message: '收费人ID不能为空', trigger: 'blur' }
          ],
          pname: [
            { required: true, message: '收费对象姓名不能为空', trigger: 'blur' }
          ],
          dname: [
            { required: true, message: '收费人姓名不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.mid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.mid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/money/info/${this.dataForm.mid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mdate = data.money.mdate
                this.dataForm.mcount = data.money.mcount
                this.dataForm.pid = data.money.pid
                this.dataForm.did = data.money.did
                this.dataForm.pname = data.money.pname
                this.dataForm.dname = data.money.dname
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/money/${!this.dataForm.mid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'mid': this.dataForm.mid || undefined,
                'mdate': this.dataForm.mdate,
                'mcount': this.dataForm.mcount,
                'pid': this.dataForm.pid,
                'did': this.dataForm.did,
                'pname': this.dataForm.pname,
                'dname': this.dataForm.dname
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
